package com.egao.texasHoldEmPoker.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.texasHoldEmPoker.biz.PlayerJetonBiz;
import com.egao.texasHoldEmPoker.entity.PlayerJeton;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-01-26 12:21:48
 * @version ：1.0
 */
@Repository
public class PlayerJetonBizImpl extends BaseDao implements PlayerJetonBiz {

	@Override
	public JsonResult list(PageResult2<T> page, PlayerJeton playerJeton, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PlayerJeton> rm = new BeanPropertyRowMapper<PlayerJeton>(PlayerJeton.class);
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.player_id");
    sql.append(",p.price,p.remark,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player_jeton p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(playerJeton)){
//			if(Utils.isNotEmpty(playerJeton.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", playerJeton.getName());
//			}
    if(Utils.isNotEmpty(playerJeton.getSort())&&Utils.isNotEmpty(playerJeton.getOrder())){
        sql.append(" order by p."+playerJeton.getSort()+" "+playerJeton.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<PlayerJeton> list(PlayerJeton playerJeton, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PlayerJeton> rm = new BeanPropertyRowMapper<PlayerJeton>(PlayerJeton.class);
    StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.player_id");
    sql.append(",p.price,p.remark,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player_jeton p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(playerJeton)){
//			if(Utils.isNotEmpty(playerJeton.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", playerJeton.getName());
//			}
      if(Utils.isNotEmpty(playerJeton.getSort())&&Utils.isNotEmpty(playerJeton.getOrder())){
				sql.append(" order by p."+playerJeton.getSort()+" "+playerJeton.getOrder());
			} else {
				sql.append(" order by p.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public PlayerJeton getById(PlayerJeton playerJeton) {
		RowMapper<PlayerJeton> rm = new BeanPropertyRowMapper<PlayerJeton>(PlayerJeton.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.player_id");
    sql.append(",p.price,p.remark,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player_jeton p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 and p.id=:id ");
		map.put("id", playerJeton.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(PlayerJeton playerJeton, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into th_player_jeton (");
    sql.append("organization_id,player_id,price,remark");
		sql.append(",order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:player_id,:price,:remark");
		sql.append(",:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("player_id", playerJeton.getPlayerId()==null?null:playerJeton.getPlayerId()+"");
    map.put("price", playerJeton.getPrice()==null?null:playerJeton.getPrice()+"");
    map.put("remark", playerJeton.getRemark());
    map.put("order_num", playerJeton.getOrderNum()==null?null:playerJeton.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(PlayerJeton playerJeton, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update th_player_jeton set ");
    sql.append("player_id=:player_id,price=:price,remark=:remark ");
		sql.append(",order_num=:order_num,update_time=now(),update_user=:update_user where id=:id ");
    map.put("player_id", playerJeton.getPlayerId()==null?null:playerJeton.getPlayerId()+"");
    map.put("price", playerJeton.getPrice()==null?null:playerJeton.getPrice()+"");
    map.put("remark", playerJeton.getRemark());
    map.put("order_num", playerJeton.getOrderNum()==null?null:playerJeton.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", playerJeton.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(PlayerJeton playerJeton, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(playerJeton.getId().length()>0){
			for(String ID : playerJeton.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update th_player_jeton set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
